<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.phone"
        placeholder="手机号"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.company"
        filterable
        class="filter-item"
        clearable
        placeholder="请选择加油站"
        @keyup.enter.native="handleFilter"
      >
        <el-option v-for="item in cooperation" :label="item.company" :value="item.company"/>
      </el-select>
      <!-- <el-input v-model="listQuery.title" placeholder="手机号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" /> -->
      <el-select
        v-model="listQuery.type"
        placeholder="类型"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option label="加油站" value="1" aria-checked=""/>
        <!-- <el-option
          label="修理站"
          value="2"
        />-->
        <el-option label="加气站" value="3"/>
        <!-- <el-option
          label="总公司"
          value="4"
        />-->
      </el-select>
      <el-select
        v-model="listQuery.state"
        placeholder="状态"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option label="开启" value="0" aria-checked=""/>
        <el-option label="关闭" value="1"/>
      </el-select>
      <!-- <el-select v-model="listQuery.type" :placeholder="$t('table.type')" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name+'('+item.key+')'" :value="item.key" />
      </el-select>
      <el-select v-model="listQuery.sort" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option v-for="item in sortOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select>-->
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >{{ $t('table.search') }}</el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >{{ $t('table.add') }}</el-button>
      <!-- <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">
        {{ $t('table.export') }}
      </el-button>
      <el-checkbox v-model="showReviewer" class="filter-item" style="margin-left:15px;" @change="tableKey=tableKey+1">
        {{ $t('table.reviewer') }}
      </el-checkbox>-->
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="tableData"
      border=""
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="序号" prop="id" align="center" width="50">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column label="姓名" min-width align="center">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="车牌" min-widthh align="center">
        <template slot-scope="scope">{{ scope.row.vehicleNumber }}</template>
      </el-table-column>
      <el-table-column label="状态" width="" align="center">
        <template slot-scope="scope">{{ scope.row.state | stateFilter}}</template>
      </el-table-column>
      <!-- <el-table-column label="类型" min-width align="center">
        <template slot-scope="scope">{{ scope.row.type | typeFilter }}</template>
      </el-table-column>-->
      <el-table-column label="创建时间" min-width align="center">
        <template slot-scope="scope">{{ scope.row.created}}</template>
      </el-table-column>
      <el-table-column label="备注" min-width align="center">
        <template slot-scope="scope">{{ scope.row.remark }}</template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="170px"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <!-- <el-button type="primary" size="mini" @click="detail(scope.row)">详情</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      top="2vh"
      width="1100px"
    >
      <addUsers ref="addUsers" :temp="temp"></addUsers>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button type="primary" @click="createData()">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import permission from "@/directive/permission/index.js"; // 权限判断指令
import checkPermission from "@/utils/permission"; // 权限判断函数

import { fetchPv, updateCooperation } from "@/api/article";
// import { user_drivers } from "@/api/user";
import { forList } from "@/api/get-list";
import { cooperationUnBind, addDriver } from "@/api/user";

import waves from "@/directive/waves"; // Waves directive
import { parseTime } from "@/utils";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
import qrCode from "@/components/qrcode";
import { Loading } from "element-ui";
import provinceData from "@/components/province";
import addUsers from "@/components/addUsers";

const calendarTypeOptions = [
  { key: "CN", display_name: "China" },
  { key: "US", display_name: "USA" },
  { key: "JP", display_name: "Japan" },
  { key: "EU", display_name: "Eurozone" }
];

// arr to obj ,such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name;
  return acc;
}, {});

export default {
  name: "ComplexTable",
  components: { Pagination, qrCode, provinceData, addUsers },
  directives: { waves, permission },
  filters: {
    typeFilter(type) {
      if (type == 1) {
        return "加油站";
      } else if (type == 2) {
        return "修理站";
      } else if (type == 3) {
        return "加气站";
      } else if (type == 4) {
        return "总公司";
      } else {
        return "";
      }
    },
    stateFilter(state) {
      switch (state) {
        case 0:
          return "注销";
        case 1:
          return "审核中";
        case 2:
          return "审核失败";
        case 3:
          return "审核成功";
        case 4:
          return "未认证";
      }
    }
  },
  data() {
    return {
      tableData: [],
      imageUrl: "",
      codeVal: "",
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        phone: "",
        name: "",
        type: "",
        sort: "+id",
        state: "",
        company: ""
      },
      showReviewer: false,
      temp: {
        name: "",
        phone: "",
        idCard: "",
        type: "",
        licensePic: "",
        vehiclelicensePic: "",
        cardPic: "",
        businessPic: ""
      },
      cooperation: {},

      dialogFormVisible: false,
      dialogCode: false,
      dialogPay: false,
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "新增"
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {},
      downloadLoading: false,
      isAdmin: true
    };
  },
  created() {
    this.getList();
  },
  methods: {
    detail(row) {
      console.log(row);
      this.$router.push({
        path: "/cooperationDetail",
        query: {
          relationId: row.id,
          relationType: 1,
          currentMoney: row.currentMoney,
          phone: row.phone,
          name: row.name,
          uuid: row.uuid
        }
      });
    },

    getList() {
      this.listLoading = true;
      console.log(this.listLoading);
      forList(this.API.URL.DRIVERS_LIST,this.listQuery).then(res => {
        this.tableData = res.data.records;
        this.total = Number(res.data.total);

        this.listLoading = false;
      });
    },
    resetTemp() {
      this.temp = {
        name: "",
        phone: "",
        idCard: "",
        type: "",
        licensePic: "",
        vehiclelicensePic: "",
        cardPic: "",
        businessPic: ""
      };
    },

    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.$refs.addUsers.clearTemp();
    },

    createData() {
      this.$refs.addUsers.submitForm();
    },
    handleUpdate(row) {
      this.resetTemp();
      this.temp = Object.assign({}, row); // copy obj
      console.log(row);
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.$refs.addUsers.clearTemp();
    },

    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    sortChange(data) {
      const { prop, order } = data;
      if (prop === "id") {
        this.sortByID(order);
      }
    }
  }
};
</script>


